﻿using System;
using System.Collections.Specialized;
using System.Web;
using System.Linq;

namespace MyCommon
{
    /// <summary>
    /// Url参数编辑
    /// </summary>
    public class UrlParametersEditer
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">原url</param>
        public UrlParametersEditer(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException("参数不能为Null或空字符串", "url");
            }
            if (url.Contains('?'))
            {
                var array = url.Split('?');
                Path = array[0];
                QueryString = HttpUtility.ParseQueryString(array[1]);
            }
            else
            {
                Path = url;
                QueryString = HttpUtility.ParseQueryString("");
            }
        }
        /// <summary>
        /// 不包含查询字符串的路径
        /// </summary>
        public string Path
        {
            get;
            set;
        }
        /// <summary>
        /// 结果Url
        /// </summary>
        public string Url
        {
            get
            {
                if (QueryString.HasKeys())
                {
                    return Path + "?" + QueryString.ToString();
                }
                return Path;
            }
        }
        /// <summary>
        /// 查询字符串
        /// </summary>
        public NameValueCollection QueryString { get; private set; }
    }
}
